package com.se.authority;

import java.io.IOException;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.se.config.Preferences;

import com.se.common.DateUtils;
import com.se.common.StringUtils;
import com.se.model.CommonSessionUser;
public class LoginFilter implements Filter {

    public void init(FilterConfig config) throws ServletException { }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    	HttpSession session = ((HttpServletRequest)request).getSession();
    	ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(((HttpServletRequest)request).getSession().getServletContext());
    	Preferences config = (Preferences) ctx.getBean("config");
    	String reqURI = config.getString("mstr.config.login-url");
//    	
//    	String st_time = config.getString("mstr.config.st-time-out");
//    	String ed_time = config.getString("mstr.config.ed-time-out"); 
//    	String ceo_ad = config.getString("mstr.config.ceo-admin"); 
//    	String[] ceo_admin = ceo_ad.split("\\|");
//    	String exec = "";
        try{
        	 System.out.println("==============================Login filter ============================================================================");
        	
        	CommonSessionUser user = (CommonSessionUser)((HttpServletRequest)request).getSession().getAttribute(CommonSessionUser.ATTRIBUTE_NAME);
         	
//         	if (Integer.parseInt(DateUtils.getCurrentTimeString().substring(0, 4)) >= Integer.parseInt(st_time) 
//         			&& Integer.parseInt(DateUtils.getCurrentTimeString().substring(0, 4)) <= Integer.parseInt(ed_time)){
//         		
//         		for (int i=0 ; i < ceo_admin.length; i++){
//         		
//	         		if (user.getMstrId().equals(ceo_admin[i])){
//	         			exec = "ok";
//	         		}
//         		}
//         		
//         		if ("ok".equals(exec)){
//         			System.out.println("로그인불가능인 시간이지만 admin계정입니다. 로그인 가능");
//         		}else{
//         			System.out.println("로그인불가능시간 ");
//             		session.setAttribute("ERRCODE", "2");
//             		((HttpServletResponse)response).sendRedirect(reqURI);
//                    return;	
//         		}
//         	}else{
//         		System.out.println("로그인가능시간 ");
//         	}
         	
        	 System.out.println("user===   " + user);
        	
        	// session에 사용자 정보가 없으면  Login 페이지로 보냄.
            if(user == null) {
            	
            	
            	((HttpServletResponse)response).sendRedirect(reqURI);
               return;	
            	
            }
        }catch(Exception e){
            e.printStackTrace();
            ((HttpServletResponse)response).sendRedirect(reqURI);
            session.setAttribute("ERROR_MSG", e.getMessage());
            return;
        }

        chain.doFilter(request, response);
    }

    public void destroy() { }
}